package jsonTest;

import java.util.List;

import handler.Handler;
import json.Json_Account;
import json.Json_Company;
import json.Json_Product;
import jsonTest.Mgr.Company;
import jsonTest.MyCompany.GetMyCompanyCommand;

public class BaseCompany {
	protected static final String TAG = "BaseCompany";
	
	public BaseCompany(){
		this.util = new JsonUtil();
	}

	public interface OnDataReady {
		public void setData(Object obj);
	}
	
	public JsonUtil util;

	OnDataReady dataReady;
	
//	Handler handler;
	Json_Company company;
	
	public void setCompany(Json_Company company){
		this.company = company;
	}
	public Json_Company getCompany(){
		return this.company;
	}

	public void executeCmd(final Command cmd) {
		Thread t = new Thread(new Runnable(){
			@Override
			public void run() {
				cmd.execute();
				return;
			}			
		});
		t.start();
	}
	public void getProductList(final Json_Company company, final OnDataReady dataReady) {
		this.dataReady = dataReady;

		GetProductListCommand getmycompanycmd = new GetProductListCommand(company);
		executeCmd(getmycompanycmd);
	}

	class GetProductListCommand implements Command {	
		Json_Company com;

		public GetProductListCommand(Json_Company company) {
			this.com = company;
		}

		@Override
		public void execute() {
			try {
				List<Json_Product> products = util.getProductList(company.getId());
				if (dataReady != null)
					dataReady.setData(products);
			} catch (Exception e) {
				e.printStackTrace();
				if (dataReady != null)
					dataReady.setData(null);
			}
		}
	}
}